package com.helloags;

import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerLog;
import com.esri.arcgis.server.IServerLogQuery;
import com.esri.arcgis.server.IServerObjectAdmin2;
import com.esri.arcgis.server.IServerObjectAdmin2Proxy;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.ServerInitializer;

/**
 * 获得Server的日志信息
 * @author zhushijie
 *
 */
public class GetServerLog {

	public static void main(String[] args) throws AutomationException, IOException, DocumentException {
		ServerInitializer si = new ServerInitializer();
        si.initializeServer("*", "zhushijie", "zsj123456");
        
		ServerConnection conn = new ServerConnection(); 
		conn.connect("192.168.3.106", "*", "zhushijie", "zsj123456");
//		conn.connect("192.168.3.105", "*", "administrator", "111111");
		
		for(int i = 0 ; i < 500; i = i + 100){
			IServerObjectAdmin2 soa = new IServerObjectAdmin2Proxy(conn.getServerObjectAdmin());
		IServerLog log = soa.getServerLog();
//		logQuery.setLevel(2);
//		GregorianCalendar calendar = new GregorianCalendar(2012,1,1);
//		
//		Date st = calendar.getTime();
//		Date et = new Date(System.currentTimeMillis());
			IServerLogQuery logQuery = log.createQuery();
			logQuery.setStartIndex(i);
			logQuery.setMaxRecords(3);
		
//		logQuery.setStartTime(st);
//		logQuery.setEndTime(et);
		
		
		String logRecords = log.getLogRecords(logQuery);
//		String logRecords = "<Log><Msg time='2012-09-03T17:44:27' type='ERROR'>Configuration search.MapServer can not be started.</Msg></Log>";
		
//		Document doc = DocumentHelper.parseText(logRecords);
//		Element root = doc.getRootElement();
//		Iterator iter = root.elementIterator();
		
		System.out.println(logRecords);
//		while(iter.hasNext()){
//			Element ele = (Element)iter.next();
//			System.out.print("time: " + ele.attributeValue("time"));
//			System.out.print("\tlevle:" + ele.attributeValue("type"));
//			System.out.println("\tMessage:" + ele.getText());
//		}
		System.out.println("***************************************************");
		}
//		System.out.println(root.nodeCount()-1);//log数量。不知道什么原因，返回值总是比实际值大1，故这里减1
		conn.release();
//		System.out.println(logRecords);
	}

}
